package cn.com.wangluotongxin.netty.event;

import cn.com.wangluotongxin.netty.constants.MsgType;
import cn.com.wangluotongxin.netty.constants.Result;
import cn.com.wangluotongxin.netty.pojo.NettyResponse;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class HeartBeatEventHandler implements ApplicationListener<HeartBeatEvent> {

    @Override
    public void onApplicationEvent(HeartBeatEvent event) {
        NettyResponse response = new NettyResponse()
                .setMsgType(MsgType.HEART_BEAT_RESPONSE)
                .setResult(Result.SUCCESS.getCode())
                .setDateTime(DateUtil.date().getTime());
        event.getChannel().writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(response)));
    }
}
